package cn.t.facade;


import cn.t.constants.CommonConstants;
import cn.t.converter.api.UnitDTOConverter;
import cn.t.converter.order.RegionVOConverter;
import cn.t.core.enums.EnumData;
import cn.t.core.enums.EnumStore;
import cn.t.model.common.UnitDO;
import cn.t.model.order.RegionDO;
import cn.t.service.common.IUploadService;
import cn.t.service.common.UnitService;
import cn.t.service.order.RegionService;
import cn.t.vo.UnitVO;
import cn.t.vo.order.RegionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通用接口服务编排
 *
 * @author t
 * @since 2024-05-24 10:19:33
 */
@Slf4j
@Service
public class CommonFacade {

    @Resource
    private RegionService regionService;
    @Resource
    private RegionVOConverter regionVOConverter;
    @Resource(name = "ossUploadService")
    private IUploadService uploadService;
    @Resource
    private UnitService unitService;
    @Resource
    private UnitDTOConverter unitConverter;

    public Map<String, List<EnumData>> getDicMap(List<String> enumNames) {
        Map<String, List<EnumData>> subDicMap = new HashMap<>();

        if (CollectionUtils.isEmpty(enumNames)) {
            return EnumStore.getAll();
        }

        EnumStore.getAll().forEach((key, val) -> {
            if (enumNames.contains(key)) {
                subDicMap.put(key, val);
            }
        });

        return subDicMap;
    }

    public List<RegionVO> getRegion(String parentCode) {

        List<RegionDO> regions = this.regionService.list(parentCode);
        if (CollectionUtils.isEmpty(regions)) {
            return Collections.emptyList();
        }

        // 指定parentCode 只查询下级区域
        if (StringUtils.hasText(parentCode)) {
            List<RegionVO> regionData = new ArrayList<>(regions.size());
            regions.forEach(region -> {
                RegionVO data = new RegionVO(region.getRegionCode(), region.getRegionName(), null);
                regionData.add(data);
            });
            return regionData;
        } else {
            // 查询省市区树
            List<RegionVO> provinceVOs = getRegionVOS(regions, CommonConstants.STR_ZERO);
            // 填充下级
            provinceVOs.forEach(p -> {
                List<RegionVO> cityVOs = getRegionVOS(regions, p.getValue());
                if (CollectionUtils.isEmpty(cityVOs)) {
                    return;
                }

                cityVOs.forEach(c -> {
                    List<RegionVO> regionVOs = getRegionVOS(regions, c.getValue());
                    if (CollectionUtils.isEmpty(regionVOs)) {
                        return;
                    }
                    c.setChildren(regionVOs);
                });
                p.setChildren(cityVOs);
            });
            return provinceVOs;
        }
    }

    public String upload(MultipartFile file) {
        return uploadService.upload(file);
    }

    private List<RegionVO> getRegionVOS(List<RegionDO> regions, String parentRegionCode) {
        List<RegionDO> regionDOs = regions.stream().filter(pc -> parentRegionCode.equals(pc.getParentCode())).sorted(Comparator.comparing(RegionDO::getSort)).collect(Collectors.toList());
        List<RegionVO> regionVOs = this.regionVOConverter.do2Vo(regionDOs);
        if (CollectionUtils.isEmpty(regionVOs)) {
            return Collections.emptyList();
        }
        return regionVOs;
    }

    public List<UnitVO> getUnit(String type) {
        List<UnitDO> unitDOS= unitService.getUnit(type);
        return this.unitConverter.do2Vo(unitDOS);
    }

}

